package sum.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sum.Entity.Person;
import sum.Entity.User;
import org.springframework.ui.Model;
import sum.Service.UserService;
@Controller
@RequestMapping("/main")
public class MainController {
    public Person person;
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String Login(User user)
    {
        User user1 = userService.check(user);
        if (user1 != null) {
            person = new Person(user1.getUsername(),user1.getPassword(),user1.getRole());
            return "mainmenu";
        } else
        {
            return "error";
        }
    }
    @RequestMapping("/register")
    public String Register(User user) {
        User user2 = userService.checkusername(user.getUsername());
        if (user2 == null) {
            userService.adduser(user);
            return "registerok";
        }
        else
        {
            return "error";
        }
    }
    @RequestMapping("/changepassword")
    public String changepassword(User user)
    {
       User user3=userService.checkusername(user.getUsername());
       if(user3 != null)
       {
       userService.changepwd(user);
          return "changepasswordok";
       }
       else
       {
           return "error";
       }
    }
    @RequestMapping("/logout")
    public String logout()
    {
        person.setRole("null");
        return "login";
    }
    @RequestMapping("/student")
    public String student()
    {
        if("0".equals(person.getRole()))
        {
            return "admin";
        }
        else if("1".equals(person.getRole()))
        {
            return "teacher";
        }
        else if("2".equals(person.getRole()))
        {
            return "student";
        }
        else
        {
            return "error";
        }
    }
    @RequestMapping("/user")
    public String user()
    {
        if("0".equals(person.getRole()))
        {
            return "user";
        }
        else
        {
            return "roleerror";
        }
    }
}
